package icst.cyber.hrm.communicationDB;

import java.io.InputStream;
import java.sql.DriverManager;
import java.util.Properties;
import org.apache.log4j.Logger;

/**
 * This class uses to get database connection
 * 
 * @author NGOCLAN
 */
public class Connection {
	/**
	 * props property
	 */
	private static Properties props = new Properties();

	/**
	 * log property
	 */
	private static Logger log = Logger.getLogger(Connection.class);

	// read db.properties file as input stream and load into Properties object
	static {
		try {
			InputStream is = Connection.class
					.getResourceAsStream("/icst/cyber/hrm/properties/db.properties");
			props.load(is);
		} catch (Exception ex) {
			// in case occur exception, log it
			log.error("Load db.properties", ex);
		}
	}

	public static java.sql.Connection getConnection() {
		// load the driver and establish a connection
		try {
			String jdbcDriver = props.getProperty("jdbcDriver");
			String jdbcUrl = props.getProperty("jdbcUrl");
			String jdbcUser = props.getProperty("jdbcUser");
			String jdbcPassword = props.getProperty("jdbcPassword");

			Class.forName(jdbcDriver);
			return (java.sql.Connection) DriverManager.getConnection(jdbcUrl,
					jdbcUser, jdbcPassword);
		} catch (Exception ex) {
			// in case occur exception, log it
			log.error("getConnection", ex);
		}

		return null;
	}
}
